package com.doublefish.doublefishrobot.ui.home.s5;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;

import com.doublefish.doublefishrobot.R;
import com.doublefish.doublefishrobot.base.BaseActivity;
import com.doublefish.doublefishrobot.utils.AppCacheUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 作者:dugaofeng on 2025/3/14 14:26
 */
public class S5ModeSettingActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView titleTV;
    //方案1
    @BindView(R.id.iv_select)
    ImageView imageView;

    @BindView(R.id.seekbar)
    SeekBar seekBar;

    @SuppressLint("UseSwitchCompatOrMaterialCode")
    @BindView(R.id.switchbtn)
    Switch aSwitchBtn;

    //方案2
    @BindView(R.id.iv_select2)
    ImageView imageView2;

    @BindView(R.id.tv_course)
    TextView courseTv;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mode_s5_setting;
    }

    @Override
    protected void initView() {
        titleTV.setText(R.string.bg_setting);
    }

    @Override
    protected void initData() {
        // 超5的后台运行方案 默认0
        int bgPrograme = AppCacheUtil.getInstance().getBgProgram();
        // 超5的后台运行方案 亮度
        float bgLight = AppCacheUtil.getInstance().getBgLight();
        // 超5的后台运行方案 播放MP3
        boolean bgPlayer = AppCacheUtil.getInstance().isBgPlayer();

        if (bgPrograme == 0) {
            imageView.setSelected(true);
            imageView2.setSelected(false);
        } else {
            imageView.setSelected(false);
            imageView2.setSelected(true);
        }
        //bgLight 的范围0 - 0.3
        seekBar.setProgress((int)(bgLight * 100 / 0.3));
        aSwitchBtn.setChecked(bgPlayer);

        SpannableString help = new SpannableString(getString(R.string.course));
        help.setSpan(new UnderlineSpan(), 0, help.length(), 0);
        courseTv.setText(help);
    }

    @Override
    protected void initListener() {
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                //bgLight 的范围0 - 0.3
                float bgLight= seekBar.getProgress() * 0.3f / 100;
                AppCacheUtil.getInstance().setBgLight(bgLight);
            }
        });

        aSwitchBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                boolean isBgPlayer = AppCacheUtil.getInstance().isBgPlayer();
                if (b == isBgPlayer) {
                    return;
                }
                AppCacheUtil.getInstance().setBgPlayer(b);
            }
        });
    }

    private void selectBgPrograme(int program) {
        int bgProgram = AppCacheUtil.getInstance().getBgProgram();
        if (bgProgram == program) {
            return;
        }
        AppCacheUtil.getInstance().setBgProgram(program);
        if (program == 0) {
            imageView.setSelected(true);
            imageView2.setSelected(false);
        } else {
            imageView.setSelected(false);
            imageView2.setSelected(true);
        }
    }

    @OnClick({R.id.iv_select, R.id.iv_select2})
    public void onBtnClick(View view){
        switch (view.getId()) {
            case R.id.iv_select:
                selectBgPrograme(0);
                break;

            case R.id.iv_select2:
                selectBgPrograme(1);
                break;
        }
    }

    @OnClick(R.id.tv_course)
    public void onCourseBtnClick() {
        Intent intent = new Intent(this, S5ModeBgCourseActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.tv_back)
    public void onBackClick() {
        finish();
    }

}
